const http = require("../../utils/http");

// pages/item/item.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemObj: {},
    seasonStr: "",
    typeStr: "",
    swiperIndex: 1,
    gaiyaotype: true,
    xiangxitype: false,
    jiagetype: false,
    gaiyaoTop: 0,
    xiangxiTop: 0,
    jiageTop: 0,
    type: false
  },
  swiperChange(ev) {
    this.setData({
      swiperIndex: ev.detail.current + 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.id);
    http.request(`/product/product/getProductDetails/${options.id}`, "", "get").then(res => {
      console.log(res);
      this.setData({
        itemObj: res.data,
        seasonStr: res.data.product_base_info.product_season.map(item => item.season).join(","),
        typeStr: res.data.product_base_info.product_type.map(item => item.type).join(",")
      })
      wx.createSelectorQuery().select('#gaiyao').boundingClientRect(rect => {
        console.log(rect);
        this.setData({
          gaiyaoTop: rect.top
        })
      }).exec();
      wx.createSelectorQuery().select('#xiangqing').boundingClientRect(rect => {
        console.log(rect);
        this.setData({
          xiangxiTop: rect.top
        })
      }).exec();
      wx.createSelectorQuery().select('#jiage').boundingClientRect(rect => {
        console.log(rect);
        this.setData({
          jiageTop: rect.top
        })
      }).exec();
    })
  },
  scrollToGaiyao() {
    wx.pageScrollTo({
      scrollTop: this.data.gaiyaoTop,
      duration: 500,
    })
  },
  scrollToXiangxi() {
    wx.pageScrollTo({
      scrollTop: this.data.xiangxiTop,
      duration: 500,
    })
  },
  makeCall() {
    wx.makePhoneCall({
      phoneNumber: '13063115992',
    })
  },
  goHome() {
    wx.navigateBack();
  },
  scrollToTop() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 100
    })
  },
  scrollToJiage() {
    wx.pageScrollTo({
      scrollTop: this.data.jiageTop,
      duration: 500,
    })
  },
  onPageScroll(ev) {
    let top = ev.scrollTop + 50;
    const { gaiyaoTop, xiangxiTop, jiageTop } = this.data;
    if (top >= gaiyaoTop) {
      this.setData({
        type: true
      })
    }
    else {
      this.setData({
        type: false
      })
    }
    if (top >= gaiyaoTop && top < xiangxiTop) {
      this.setData({
        gaiyaotype: true,
        xiangxitype: false,
        jiagetype: false
      })
    }
    else if (top >= xiangxiTop && top < jiageTop) {
      this.setData({
        gaiyaotype: false,
        xiangxitype: true,
        jiagetype: false
      })
    }
    else {
      this.setData({
        gaiyaotype: false,
        xiangxitype: false,
        jiagetype: true
      })
    }


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})